package com.kedacom.freedomLabel.view.activity.printer;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.kedacom.freedomLabel.Options;
import com.kedacom.freedomLabel.R;
import com.kedacom.freedomLabel.base.BaseActivity;
import com.kedacom.freedomLabel.databinding.ActivityPrinterPrinterSettingBinding;
import com.kedacom.freedomLabel.model.annotation.PrinterSettingCategory;
import com.kedacom.freedomLabel.model.bean.Template;
import com.kedacom.freedomLabel.printer.GpPrinterOptions;
import com.kedacom.freedomLabel.viewmodel.printer.PrinterSettingViewModel;
import com.kedacom.lego.annotation.ContentView;
import com.kedacom.lego.annotation.OnMessage;
import com.kedacom.lego.annotation.ViewModel;
import com.kedacom.lego.message.LegoEventBus;


@ContentView(R.layout.activity_printer_printer_setting)
@ViewModel(PrinterSettingViewModel.class)
public class PrinterSettingActivity extends BaseActivity<ActivityPrinterPrinterSettingBinding, PrinterSettingViewModel> {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LegoEventBus.use(Options.MSG_UPDATE_TEMPLATE, Template.class)
                .observe(this, template -> {
                    if (template == null) {
                        showDialog("请选择模板", "选择模板", (dialog, which) -> {
                            beep();
                            if (which == DialogInterface.BUTTON_POSITIVE) {
                                Intent intent = new Intent(PrinterSettingActivity.this,
                                        LabelSelectActivity.class);
                                startActivity(intent);
                            }
                        });
                    } else {
                        mBinding.gpLabelTemplate.setContentText(template.type);
                        mBinding.gpLabelWidth.setContentText(String.format("%s%s", template.width, "mm"));
                        mBinding.gpLabelHeight.setContentText(String.format("%s%s", template.height, "mm"));
                    }
                });
    }

    @OnMessage(value = Options.MSG_SELECT_PRINTER_SETTING_ITEM, always = true)
    public void receiveSelectSettingItemMessage(@PrinterSettingCategory String category) {
        beep();
        Intent intent = new Intent(getContext(), GpPrinterSettingSelectActivity.class);
        intent.putExtra(PrinterSettingCategory.class.getSimpleName(), category);
        startActivity(intent);
    }

    // 返回
    public void onBackClick(View view) {
        beep();
        finish();
    }

    // 保存
    public void onSaveClick(View view) {
        beep();
    }


    @OnMessage(value = Options.MSG_SELECT_ITEM + "_" + PrinterSettingCategory.GAP, always = true)
    public void receiveSelectedGAPItem(int directionIndex) {
        String value = GpPrinterOptions.getGpPrinterOptionValue(PrinterSettingCategory.GAP, directionIndex);
        mBinding.gpPrinterSelectGap.setContentText(value);
    }

    @OnMessage(value = Options.MSG_SELECT_ITEM + "_" + PrinterSettingCategory.DIRECTION, always = true)
    public void receiveSelectedDirectionItem(int directionIndex) {
        String value = GpPrinterOptions.getGpPrinterOptionValue(PrinterSettingCategory.DIRECTION, directionIndex);
        mBinding.gpPrinterSelectDirection.setContentText(value);
    }

    @OnMessage(value = Options.MSG_SELECT_ITEM + "_" + PrinterSettingCategory.DENSITY, always = true)
    public void receiveSelectedDensityItem(int densityIndex) {
        String value = GpPrinterOptions.getGpPrinterOptionValue(PrinterSettingCategory.DENSITY, densityIndex);
        mBinding.gpPrinterSelectDensity.setContentText(value);
    }

    @OnMessage(value = Options.MSG_SELECT_ITEM + "_" + PrinterSettingCategory.SPEED, always = true)
    public void receiveSelectedSpeedItem(int speedIndex) {
        String value = GpPrinterOptions.getGpPrinterOptionValue(PrinterSettingCategory.SPEED, speedIndex);
        mBinding.gpPrinterSelectSpeed.setContentText(value);
    }
}